package co.crossoft.daoImpl;

import co.crossoft.base.common.JsonResult;
import co.crossoft.domain.Resource;
import co.crossoft.mapper.ResourceMapper;
import co.crossoft.util.JsonConvertUtil;
import co.crossoft.util.JsonReturnMsg;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Snow on 8/22/2017.
 */

@Repository("resourceDaoImpl")
public class ResourceDaoImpl {

    @Autowired
    ResourceMapper resourceMapper;

    public JsonResult addOrUpdateResource(Resource resource) {
        JsonResult jsonResult = new JsonResult("S","success");
        if (resource.getResource().equals("") || resource.getPlant().equals("")) {
            jsonResult.setResultCode("F");
            jsonResult.setResultMsg("plant or resource ca not be empty!");
        } else {
            Map map = new HashMap();
            map.put("resourceName", resource.getResource());
            map.put("plant", resource.getPlant());
            List list = resourceMapper.resourceIsExist(map);
            if (list.size() > 0) {
                resourceMapper.updateResource(resource);
            } else {
                resourceMapper.addResource(resource);
            }
        }
        return jsonResult;
    }

    public JsonResult deleteResource(String handle) {
        JsonResult jsonResult = new JsonResult("S","success");
        if (handle.equals("")) {
            jsonResult.setResultCode("F");
            jsonResult.setResultMsg("primary key can not be empty!");
        } else {
            resourceMapper.deleteByPrimaryKey(handle);
        }
        return jsonResult;
    }

    public JsonResult getResourceList(String plant, String resource) {
        JsonResult jsonResult = new JsonResult("S","success");
        Map parMap = new HashMap();
        parMap.put("plant", plant);
        parMap.put("resource", resource);
        List list = resourceMapper.selectByResource(parMap);
        jsonResult.setResult(list);
        return jsonResult;
    }

    public JsonResult selectOneResource(String handle) {
        JsonResult jsonResult = new JsonResult("S","success");
        if (handle.equals("")) {
            jsonResult.setResultCode("F");
            jsonResult.setResultMsg("primary key can not be empty!");
        } else {
            List list = resourceMapper.selectOneResourceByHandle(handle);
            if (list.size() > 0) {
                jsonResult.setResult(list);
            } else {
                jsonResult.setResultCode("F");
                jsonResult.setResultMsg("no data return!");
            }
        }
        return jsonResult;
    }
}
